package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    // 处理查询所有部门请求
    // /systemDictionary/list 目前不带参数, 响应 HTML 格式内容
    @RequiredPermission(name="部门页面", expression = "systemDictionary:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        // 获取所有部门数据, 存到 Model 中
        model.addAttribute("pageInfo", systemDictionaryService.query(qo));
        // 找视图
        return "/systemDictionary/list"; // /WEB-INF/views/systemDictionary/list.jsp
    }

    // /systemDictionary/delete?id=id值
    // 处理查询所有部门删除的请求,希望浏览器重新发送一次请求再查询一次
    @RequiredPermission(name="员工新增/编辑", expression = "systemDictionary:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list"; // 让浏览器再发送一次请求 localhost/systemDictionary/list
    }

    // 处理保存的请求 /systemDictionary/saveOrUpdate, POST 携带参数
    @RequiredPermission(name="员工新增/编辑", expression = "systemDictionary:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary) {
        if (systemDictionary.getId() != null) {
            systemDictionaryService.update(systemDictionary);
        }else {
            systemDictionaryService.save(systemDictionary);
        }
        return "redirect:/systemDictionary/list";
    }
}
